/*
 *-----------------------------------------------------------------------------
 *
 * TUMLEditorDispatcher.java
 *
 * This file was created from IUMLEditor.idl
 * by the Simple JavRMI backend.
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *-----------------------------------------------------------------------------
 */

package com.googlecode.umlsynch.rpc;

import java.io.*;

import com.googlecode.umlsynch.rpc.base.TIOHandler;

import com.googlecode.umlsynch.rpc.base.IDebug;

import com.googlecode.umlsynch.rpc.base.TDebug;

import com.googlecode.umlsynch.rpc.base.IDispatcher;

import com.googlecode.umlsynch.rpc.TWorkingAreaDescriptor;

import com.googlecode.umlsynch.rpc.TPointDescriptor;

import com.googlecode.umlsynch.rpc.TFileDescriptor;

import com.googlecode.umlsynch.rpc.TPacketDescriptor;

import com.googlecode.umlsynch.rpc.TEnumDescriptor;

import com.googlecode.umlsynch.rpc.TClassDescriptor;

import com.googlecode.umlsynch.rpc.IUMLEditor;


public class TUMLEditorDispatcher implements IDispatcher
{
  private IUMLEditor implementation = null;
  private TDebug debug;
  
  public TUMLEditorDispatcher(IDebug dbg, IDispatcher disp, IUMLEditor impl) {
    debug = new TDebug(dbg);
    if (disp != null)
      disp.register(this);
    
    implementation = impl;
  }
  public boolean match(String interfaceName) {
    if (interfaceName.equals("IUMLEditor"))
      return true;
    return false;
  }
  
  public void register(IDispatcher disp) {
    disp.register(this);
  }
  
  public void process(TIOHandler handler) {
    String data = handler.readString();
    
    debug.Note("Recieved methodname:" + data);
    
    
    if (data.equals( "GetWorkingArea")) {
      GetWorkingArea(handler);
      return;
    }
    
    if (data.equals( "CreatePacketOnDiagram")) {
      CreatePacketOnDiagram(handler);
      return;
    }
    
    if (data.equals( "CreateClassOnDiagram")) {
      CreateClassOnDiagram(handler);
      return;
    }
    
    if (data.equals( "CreateEnumOnDiagram")) {
      CreateEnumOnDiagram(handler);
      return;
    }
    
    debug.Error("Unknown method name:" + data);
  }
  
  private void GetWorkingArea(TIOHandler handler) {
    String data = null;
    TWorkingAreaDescriptor result = null;
    
    
    try {
      result = implementation.GetWorkingArea();
      handler.getObjectOutputStream().writeObject(new String("TWorkingAreaDescriptor"));
      handler.getObjectOutputStream().writeObject(result);
    }
    catch (IOException e) {
    }
  }; // 
  
  
  private void CreatePacketOnDiagram(TIOHandler handler) {
    String data = null;
    TPacketDescriptor desc;
    TPointDescriptor position;
    
    
    try {
      data = handler.readString();
      if (data.equals("TPacketDescriptor")) {
        desc = (TPacketDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TPacketDescriptor");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("TPointDescriptor")) {
        position = (TPointDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TPointDescriptor");
        throw new ClassNotFoundException();
      }
      
      implementation.CreatePacketOnDiagram(desc, position);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void CreateClassOnDiagram(TIOHandler handler) {
    String data = null;
    TClassDescriptor desc;
    TPointDescriptor position;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassDescriptor")) {
        desc = (TClassDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("TPointDescriptor")) {
        position = (TPointDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TPointDescriptor");
        throw new ClassNotFoundException();
      }
      
      implementation.CreateClassOnDiagram(desc, position);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void CreateEnumOnDiagram(TIOHandler handler) {
    String data = null;
    TEnumDescriptor desc;
    TPointDescriptor position;
    
    
    try {
      data = handler.readString();
      if (data.equals("TEnumDescriptor")) {
        desc = (TEnumDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TEnumDescriptor");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("TPointDescriptor")) {
        position = (TPointDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TPointDescriptor");
        throw new ClassNotFoundException();
      }
      
      implementation.CreateEnumOnDiagram(desc, position);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
}; // IUMLEditor

